package cn.com.wantools.service.provider;

import cn.com.wantools.model.loanrate.LoanRateInfo;
import cn.com.wantools.model.loanrate.UcLoanRate;
import cn.com.wantools.service.UcLoanRateService;
import cn.hutool.core.collection.ListUtil;
import io.jboot.aop.annotation.Bean;
import io.jboot.db.model.Columns;
import io.jboot.service.JbootServiceBase;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Bean
public class UcLoanRateServiceProvider extends JbootServiceBase<UcLoanRate> implements UcLoanRateService {
    @Override
    public List<LoanRateInfo> queryLoanRateByType(String type) {

        List<UcLoanRate> ucLoanRateList =  findListByColumns(Columns.create().eq("type", type), "gmt_create desc");
        Map<String, LoanRateInfo> loanRateMap = new HashMap<>();
        if (ucLoanRateList != null) {
            ucLoanRateList.forEach(item -> {
                if (loanRateMap.get(item.getType()+ item.getGtYear() + item.getLeYear()) == null) {
                    LoanRateInfo loanRateInfo = new LoanRateInfo();
                    loanRateInfo.setType(item.getType());
                    loanRateInfo.setGtYear(item.getGtYear());
                    loanRateInfo.setLeYear(item.getLeYear());
                    loanRateInfo.setLoanRate(item.getLoanRate());
                    loanRateInfo.setGmtCreate(item.getGmtCreate());
                    loanRateInfo.setRemark(item.getRemark());
                    loanRateMap.put(item.getType()+ item.getGtYear() + item.getLeYear(), loanRateInfo);
                }
            });
            return ListUtil.toList(loanRateMap.values());
        }
        return null;
    }

    @Override
    public List<LoanRateInfo> queryAllLoanRate() {
        List<UcLoanRate> ucLoanRateList =  findAll();
        if (ucLoanRateList != null) {
            ucLoanRateList = ucLoanRateList.stream().sorted((x, y) -> y.getGmtCreate().compareTo(x.getGmtCreate())).collect(Collectors.toList());
            Map<String, LoanRateInfo> loanRateMap = new HashMap<>();

            ucLoanRateList.forEach(item -> {
                if (loanRateMap.get(item.getType()+ item.getGtYear() + item.getLeYear()) == null) {
                    LoanRateInfo loanRateInfo = new LoanRateInfo();
                    loanRateInfo.setType(item.getType());
                    loanRateInfo.setGtYear(item.getGtYear());
                    loanRateInfo.setLeYear(item.getLeYear());
                    loanRateInfo.setLoanRate(item.getLoanRate());
                    loanRateInfo.setGmtCreate(item.getGmtCreate());
                    loanRateInfo.setRemark(item.getRemark());
                    loanRateMap.put(item.getType() + item.getGtYear() + item.getLeYear(), loanRateInfo);
                }
            });
            return ListUtil.toList(loanRateMap.values());
        }
        return null;
    }

    @Override
    public void saveLoanRate(LoanRateInfo loanRateInfo) {
        UcLoanRate ucLoanRate = new UcLoanRate();
        ucLoanRate.setRemark(loanRateInfo.getRemark());
        ucLoanRate.setLoanRate(loanRateInfo.getLoanRate());
        ucLoanRate.setGmtCreate(loanRateInfo.getGmtCreate());
        ucLoanRate.setGtYear(loanRateInfo.getGtYear());
        ucLoanRate.setLeYear(loanRateInfo.getLeYear());
        ucLoanRate.setType(loanRateInfo.getType());
        saveOrUpdate(ucLoanRate);
    }
}
